home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / What's New? / Development Kits / Speech Recognition 1.5a1 / SR Sample Code / Tiny SR Example / MPW C Tiny Example / SpeechRecognition.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-17  |  18.8 KB  |  420 lines  |  [TEXT/MPS ]

  1. /************************************************************
  2.  
  3. Created: Monday, January 27, 1992 at 10:19:57 AM
  4.     SpeechRecognition.h
  5.     C Interface to the Apple Speech Recognition Toolbox
  6.  
  7.         Copyright Apple Computer, Inc.    1992-1995
  8.         All rights reserved
  9.  
  10. ************************************************************/
  11.  
  12.  
  13. #ifndef        __SPEECHRECOGNITION__
  14. #define        __SPEECHRECOGNITION__
  15.  
  16. #ifndef        __MEMORY__
  17. #include    <Memory.h>
  18. #endif
  19.  
  20. #ifndef    gestaltSpeechRecognitionVersion
  21.  
  22. /* need to put these into GestaltEqu.h */
  23.  
  24. #define    gestaltSpeechRecognitionVersion     'srtb'
  25. #define    gestaltSpeechRecognitionAttr        'srta'
  26.  
  27. #define    gestaltDesktopSpeechRecognition        (1L<<0)
  28. #define    gestaltTelephoneSpeechRecognition    (1L<<1)
  29.  
  30. #endif
  31.  
  32.  
  33. /* Error Codes [Speech recognition gets -5100 through -5199] */
  34.  
  35. enum {
  36.     eSRNotAvailable                = -5100,    /* the service requested is not avail or applicable */
  37.     eSRInternalError            = -5101,    /* a system internal or hardware error condition */
  38.     eSRComponentNotFound        = -5102,    /* a needed system resource was not located */
  39.     eSROutOfMemory                = -5103,    /* an out of memory error occurred in the toolbox memory space */
  40.     eSRNotASpeechObject            = -5104,    /* the object specified is no longer or never was valid */
  41.     eSRBadParameter                = -5105,    /* an invalid parameter was specified */
  42.     eSRParamOutOfRange            = -5106,    /* when we say 0-100, don't pass in 101. */
  43.     eSRBadSelector                = -5107,    /* an unrecognized selector was specified */
  44.     eSRBufferTooSmall            = -5108,    /* returned from attribute access functions */
  45.     eSRNotARecSystem            = -5109,    /* the object used was not a RecognitionSystem */
  46.     eSRFeedbackNotAvail            = -5110,    /* there is no feedback window associated with Recognizer */
  47.     eSRCantSetProperty            = -5111,    /* a non-settable property was specified */
  48.     eSRCantGetProperty            = -5112,    /* a non-gettable property was specified */
  49.     eSRCantSetDuringRecognition    = -5113,    /* the property can't be set while recognition is in progress -- do before or between utterances. */
  50.     eSRAlreadyListening            = -5114,    /* in response to SRStartListening */
  51.     eSRNotListeningState        = -5115,    /* in response to SRStopListening */
  52.     eSRModelMismatch            = -5116,    /* no acoustical models are avail to match request */
  53.     eSRNoClientLanguageModel    = -5117,    /* trying to access a non-specified LanguageModel */
  54.     eSRNoPendingUtterances        = -5118,    /* nothing to continue search on */
  55.     eSRRecognitionCanceled        = -5119,    /* an abort error occurred during search */
  56.     eSRRecognitionDone            = -5120,    /* search has finished, but nothing was recognized */
  57.     eSROtherRecAlreadyModal        = -5121,    /* another recognizer is modal at the moment, so can't set this recognizer's kBlockModally property right now */
  58.     eSRHasNoSubItems            = -5122,    /* SRCountItems or related routine was called on an object without subelements -- e.g. a word -- rather than phrase, path, or LM. */
  59.     eSRSubItemNotFound            = -5123,    /* returned when accessing a non-existent sub item of a container */
  60.     eSRLanguageModelTooBig        = -5124,    /* Cant build language models so big */
  61.     eSRAlreadyReleased            = -5125,    /* this object has already been released before */
  62.     eSRAlreadyFinished            = -5126,    /* the language model can't be finished twice */
  63.     eSRWordNotFound                = -5127,    /* the spelling couldn't be found in lookup(s) */
  64.     eSRNotFinishedWithRejection    = -5128,    /* property not found because the LMObj is not finished with rejection */
  65.     eSRExpansionTooDeep            = -5129,    /* Language model is left recursive or is embedded too many levels */
  66.     eSRTooManyElements            = -5130,    /* Too many elements added to phrase or path or other langauge model object */
  67.     eSRCantAdd                    = -5131,    /* Can't add given type of object to the base LanguageObject (e.g.in SRAddLanguageObject)    */
  68.     eSRSndInSourceDisconnected    = -5132,    /* Sound input source is disconnected */
  69.     eSRCantReadLanguageObject    = -5133,    /* An error while trying to create new Language object from file or pointer -- possibly bad format */
  70.     
  71.     /* non-release debugging error codes are included here */
  72.     eSRNotImplementedYet        = -5199        /* you'd better wait for this feature in a future release */
  73. };
  74.  
  75.  
  76. /* Type Definitions */
  77.  
  78. typedef unsigned long SpeechObject;
  79. typedef SpeechObject RecognitionSystem;
  80. typedef SpeechObject Recognizer;
  81. typedef SpeechObject SpeechSource;
  82. typedef SpeechSource RecognitionResult;
  83. typedef SpeechObject LanguageObject;    /* an LMWord, LMPhrase or LanguageModel */
  84. typedef LanguageObject LanguageModel;
  85. typedef LanguageObject LMPath;
  86. typedef LanguageObject LMPhrase;
  87. typedef LanguageObject LMWord;
  88. typedef unsigned long LMFlags;
  89. typedef unsigned short SpeedSetting;    /* between 0 and 100 */
  90. typedef unsigned short RejectionLevel;    /* between 0 and 100 */
  91.  
  92. /* When an event occurs, the user supplied proc will be called with a pointer
  93.     to the param passed in and a flag to indicate conditions such
  94.     as interrupt time or system background time.
  95. */
  96.  
  97. #if defined(powerc) || defined (__powerc)
  98. #pragma options align=mac68k
  99. #endif
  100.  
  101. struct SRCallBackStruct {
  102.     long        what;                /* one of notification flags */
  103.     long        message;            /* contains RecognitionResult id */
  104.     Recognizer    instance;            /* ID of recognizer being notified */
  105.     OSErr        status;                /* result status of last search */
  106.     short        flags;                /* non-zero if occurs during interrupt */
  107.     long        refCon;                /* user defined - set from SRCallBackParam */
  108. };
  109. typedef struct SRCallBackStruct SRCallBackStruct;
  110.  
  111. #if defined(powerc) || defined(__powerc)
  112. #pragma options align=reset
  113. #endif
  114.  
  115. /* Call back procedure definition */
  116. typedef pascal void (*SRCallBackProcPtr) (SRCallBackStruct *param);
  117.  
  118. #ifndef    USESROUTINEDESCRIPTORS
  119. #define    USESROUTINEDESCRIPTORS 0
  120. #endif
  121.  
  122. #if USESROUTINEDESCRIPTORS
  123. enum {
  124.     uppSRCallBackProcInfo = kPascalStackBased
  125.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(SRCallBackStruct*)))
  126. };
  127.  
  128. typedef UniversalProcPtr SRCallBackUPP;
  129.  
  130. #define NewSRCallBackProc(userRoutine)        \
  131.         (SRCallBackUPP) NewRoutineDescriptor((ProcPtr)(userRoutine), uppSRCallBackProcInfo, GetCurrentISA())
  132. #else
  133. typedef SRCallBackProcPtr SRCallBackUPP;
  134.  
  135. #define NewSRCallBackProc(userRoutine)        \
  136.         (SRCallBackUPP)(userRoutine)
  137. #endif
  138.  
  139. #if defined(powerc) || defined (__powerc)
  140. #pragma options align=mac68k
  141. #endif
  142.  
  143. struct SRCallBackParam {
  144.     SRCallBackUPP        callBack;
  145.     long                refCon;
  146. };
  147. typedef struct SRCallBackParam SRCallBackParam;
  148.  
  149. #if defined(powerc) || defined(__powerc)
  150. #pragma options align=reset
  151. #endif
  152.  
  153. /* Recognition System Types */
  154. #define    kDefaultRecognitionSystemID    ((OSType) 0)
  155.  
  156. /* Recognition System Properties */
  157. #define    kRejectedWord                'rejq'    /* the LMWord used to represent a rejection */
  158. #define kSeparationChars            'spch'    /* separation chars for current dialect setting -- use pointer to SeparationChars struct, defined in Dialect.h */
  159. #define    kFeedbackAndListeningModes    'fbwn'    /* (Byte) one of kNoFeedbackHasListenModes, kHasFeedbackHasListenModes, kNoFeedbackNoListenModes */
  160.                                             /*  kFeedbackAndListeningModes is only available in version 1.4 and later. */
  161.                                             /*  THIS OS TYPE MAY CHANGE in version 1.5 */
  162.  
  163. #define    kNoFeedbackNoListenModes    0        /* next allocated recognizer has no feedback window and doesn't use listening modes    */
  164. #define    kHasFeedbackHasListenModes    1        /* next allocated recognizer has feedback window and uses listening modes    */
  165. #define    kNoFeedbackHasListenModes    2        /* next allocated recognizer has no feedback window but does use listening modes    */
  166.  
  167. /* Speech Source Types */
  168. #define    kSRDefaultSpeechSource        ((OSType) 0)
  169. #define kLiveDesktopSpeechSource    'dklv'    /* live desktop sound input */
  170. #define kCannedDesktopSpeechSource    'ca22'    /* AIFF file based 16 bit, 22.050 KHz sound input */
  171.  
  172. /* Notification via Apple Event or Callback */
  173.  
  174. /* Notification Flags */
  175. #define kNotifyRecognitionBeginning    (1L<<0)    /* recognition can begin. client must now call SRContinueRecognition or SRCancelRecognition */
  176. #define kNotifyRecognitionDone        (1L<<1)    /* recognition has terminated. result (if any) is available. */
  177.  
  178. /* Apple Event selectors */
  179.  
  180. /* AppleEvent message class  */
  181. #define kAESpeechSuite                'sprc'
  182.  
  183. /* AppleEvent message event ids */
  184. #define kAESpeechDone                'srsd'
  185. #define kAESpeechDetected            'srbd'
  186.  
  187. /* AppleEvent Parameter ids */
  188. #define keyRecognizer                  'krec'
  189. #define keySpeechResult                'kspr'
  190. #define keySpeechStatus                'ksst'
  191.  
  192. /* AppleEvent Parameter types */
  193. #define typeRecognizer                'trec'
  194. #define typeSpeechResult            'tspr'
  195.  
  196.  
  197. /* Recognizer Properties */
  198. #define kSpeedVsAccuracyParam        'sped'    /* SpeedSetting between 0 and 100 */
  199.                                             /* 0 means more accurate but slower. */
  200.                                             /* 100 means (much) less accurate but faster. */
  201. #define kSearchStatusParam            'stat'    /* see status flags below */
  202. #define kNotificationParam            'noti'    /* see notification flags below */
  203. #define kCallBackParam                'call'    /* type SRCallBackParam */
  204. #define    kAutoFinishingParam            'afin'    /* automatic finishing applied on LM for search */
  205. #define kForegroundOnly                'fgon'    /* Boolean. Default is true. If true, client recognizer only active when in foreground.    */
  206. #define kBlockBackground            'blbg'    /* Boolean. Default is false. If true, when client recognizer in foreground, rest of LMs are inactive.    */
  207. #define kBlockModally                'blmd'    /* Boolean. Default is false. When true, this client's LM is only active LM; all other LMs are inactive. Be nice, don't be modal for long periods! */
  208. #define kWantsResultTextDrawn        'txfb'    /* Boolean. Default is true. If true, search results are posted to Feedback window */
  209. #define kWantsAutoFBGestures        'dfbr'    /* Boolean. Default is true. If true, client needn't call SRProcessBegin/End to get default feedback behavior */
  210. #define kSoundInVolume                'volu'    /* short in [0..100] log scaled sound input power. Can't set this property */
  211. #define kReadAudioFSSpec            'aurd'    /* *FSSpec. Specify FSSpec where raw audio is to be read (AIFF format) using kCannedDesktopSpeechSource. Reads until EOF */
  212. #define kCancelOnSoundOut            'caso'    /* Boolean: Default is true.  If any sound is played out during utterance, recognition is aborted. */
  213.  
  214. enum {
  215.     kUseToggleListen = 0,    /* listen key modes */
  216.     kUsePushToTalk
  217. };
  218.  
  219. #define kListenKeyMode            'lkmd'    /* short: either kUseToggleListen or kUsePushToTalk */
  220. #define kListenKeyCombo            'lkey'    /* short: Push-To-Talk key combination; high byte is high byte of event->modifiers, the low byte is the keycode from event->message */
  221. #define kListenKeyName            'lnam'    /* Str63: string representing ListenKeyCombo */
  222. #define kKeyWord                'kwrd'    /* Str255: keyword preceding spoken commands in kUseToggleListen mode */
  223. #define kKeyExpected            'kexp'    /* Boolean: Must the PTT key be depressed or the key word spoken before recognition can occur? */
  224.  
  225. /* Operational Status Flags */
  226. #define kIdleRecognizer                (1L<<0)    /* engine is not active */
  227. #define kSearchInProgress            (1L<<1)    /* search is in progress */
  228. #define kSearchWaitForAllClients    (1L<<2)    /* search is suspended waiting on all clients' input */
  229. #define kMustCancelSearch            (1L<<3) /* something has occurred (sound played, non-speech detected) requiring the search to abort */
  230. #define kPendingSearch                (1L<<4) /* we're about to start searching */
  231.  
  232. /* Recognition Result Properties */
  233. #define    kTEXTFormat                    'TEXT'    /* raw text in user supplied memory */
  234. #define kLMPhraseFormat                'lmph'    /* LMPhrase containing result words */
  235. #define kLMPathFormat                'lmpt'    /* LMPath containing result phrases or words */
  236. #define kLanguageModelFormat        'lmfm'    /* top level LanguageModel for post parse */
  237.  
  238. /* LanguageObject Family Properties */
  239. #define kSpelling                    'spel'    /* spelling of a LMWord or LMPhrase or LMPath, or name of a LanguageModel */
  240. #define kLMObjType                    'lmtp'    /* Returns one of LanguageObject Types listed below */
  241. #define kRefCon                        'refc'    /* 4 bytes of user storage */
  242. #define kEnabled                    'enbl'    /* Boolean -- true if LanguageObject enabled */
  243. #define kOptional                    'optl'    /* Boolean -- true if LanguageObject is optional    */
  244. #define kRepeatable                    'rptb'    /* Boolean -- true if LanguageObject is repeatable */
  245. #define kRejectable                 'rjbl'    /* Boolean -- true if LanguageObject is rejectable (Recognition System's kRejectedWord
  246.                                                 object can be returned in place of LanguageObject with this property)    */
  247. #define kPrimitive                    'prim'    /* Boolean -- determines what shows up in search result's list of primitives */
  248. #define kRejectionLevel                'rjct'    /* RejectionLevel between 0 and 100 */
  249. #define kFinishingFlags                'finf'    /* Please use kOptional, kRepeatable, and kRejetable instead. pass pointer to LMFlags. Being phased out of public interfaces. */
  250.  
  251.  
  252. /* LM Object Types -- returned as kLMObjType property of language model objects */
  253. #define kLMType                        'lmob'    /* LanguageModel */
  254. #define kPathType                    'path'    /* LMPath */
  255. #define kPhraseType                    'phra'    /* LMPhrase */
  256. #define kWordType                    'word'    /* LMWord */
  257.  
  258. /* a normal and reasonable rejection level */
  259. #define    kDefaultLMRejectionLevel     50
  260.  
  261. /* Finishing Flags - used to make up LMFlags, which is used for kFinishingFlags and kAutoFinishingParam properties */
  262. /* 4/10/95 These finishing flags are being phased out of the public interfaces.  Please use kOptional, kRepeatable,
  263.     and kRejectable Boolean LanguageObject properties instead.  Those Boolean properties are easier to use.                */
  264.                                             /* bit zero is reserved */
  265. #define kAddPauses                     (1L<<1)    /* allows user to pause between words */
  266. #define kAddRejection                 (1L<<2)    /* unrecognized words will be rejected */
  267. #define kAddNoise                    (1L<<3)    /* allows a some noise at beginning or end of utterance */
  268. #define kMakeOptional                (1L<<4)    /* contents of LM are optional */
  269. #define kMakeRepeatable                (1L<<5)    /* contents of LM may be repeated 0 or more times */
  270. #define kAddWordSpotting            (kAddRejection | kMakeRepeatable)
  271. #define kDefaultFinishing            kAddPauses
  272. #define kNoFinishing                0
  273.  
  274. /*
  275.                         NOTES ON USING THE API
  276.  
  277.     All operations (with the exception of SRGetRecognitionSystem) are
  278.     directed toward an object allocated or begot from New, Get and Read
  279.     type calls.
  280.     
  281.     There is a simple rule in dealing with allocation and disposal:
  282.     
  283.     *    all toolbox allocations are obtained from a RecognitionSystem
  284.     
  285.     *    if you obtain an object via New or Get, then you own a reference 
  286.         to that object and it must be released via SRReleaseObject when
  287.         you no longer need it
  288.     
  289.     *    when you receive a RecognitionResult object via AppleEvent or
  290.         callback, it has essentially been created on your behalf and so
  291.         you are responsible for releasing it as above
  292.     
  293.     *    when you close a RecognitionSystem, all remaining objects which
  294.         were allocated with it will be forcefully released and any
  295.         remaining references to those objects will be invalid.
  296.     
  297.     This translates into a very simple guideline:
  298.         If you allocate it or have it allocated for you, you must release
  299.         it.  If you are only peeking at it, then don't release it.
  300.  
  301. */
  302.  
  303. #if defined (__NOSRTRAPS__)
  304. #define    SRINLINE(hx)    /* eat it up */
  305. #elif defined (THREEWORDINLINE)
  306. #define    SRINLINE(hx)    THREEWORDINLINE(0x303C,hx,0xAA56)
  307. #else
  308. #define    SRINLINE(hx)    = {0x303C,hx,0xAA56}
  309. #endif
  310.  
  311.  
  312. #ifdef    __cplusplus
  313. extern "C" {
  314. #endif
  315.  
  316. /* Opening and Closing of the RecognitionSystem */
  317. pascal OSErr SROpenRecognitionSystem (RecognitionSystem *system, OSType systemID)
  318.  SRINLINE (0x0400);
  319. pascal OSErr SRCloseRecognitionSystem (RecognitionSystem system)
  320.  SRINLINE (0x0201);
  321.  
  322. /* Accessing Properties of any Speech Object */
  323. pascal OSErr SRSetProperty (SpeechObject srObject, OSType selector,
  324.         const void *property, Size propertyLen)
  325.  SRINLINE (0x0802);
  326. pascal OSErr SRGetProperty (SpeechObject srObject, OSType selector,
  327.         void *property, Size *propertyLen)
  328.  SRINLINE (0x0803);
  329.  
  330. /* Any object obtained via New or Get type calls must be released */
  331. pascal OSErr SRReleaseObject (SpeechObject srObject)
  332.  SRINLINE (0x0204);
  333. pascal OSErr SRGetReference (SpeechObject srObject, SpeechObject *newObjectRef)
  334.  SRINLINE (0x0425);
  335.  
  336. /* Traversing RecognitionResult or LMObjects */
  337. pascal OSErr SRCountItems (SpeechObject container, long *count)
  338.  SRINLINE (0x0405);
  339. pascal OSErr SRGetIndItem (SpeechObject container, SpeechObject *item, long index)
  340.  SRINLINE (0x0606);
  341. pascal OSErr SRSetIndItem (SpeechObject container, SpeechObject item, long index)
  342.  SRINLINE (0x0607);
  343. pascal OSErr SRRemoveIndItem (SpeechObject container, long index)
  344.  SRINLINE (0x0408);
  345.  
  346. /* Recognizer Instance Functions */
  347. pascal OSErr SRNewRecognizer (RecognitionSystem system, Recognizer *instance,
  348.         OSType sourceID)
  349.  SRINLINE (0x060A);
  350. pascal OSErr SRStartListening (Recognizer instance)
  351.  SRINLINE (0x020C);
  352. pascal OSErr SRStopListening (Recognizer instance)
  353.  SRINLINE (0x020D);
  354. pascal OSErr SRSetLanguageModel (Recognizer instance, LanguageModel active)
  355.  SRINLINE (0x040E);
  356. pascal OSErr SRGetLanguageModel (Recognizer instance, LanguageModel *active)
  357.  SRINLINE (0x040F);
  358. pascal OSErr SRContinueRecognition (Recognizer instance)
  359.  SRINLINE (0x0210);
  360. pascal OSErr SRCancelRecognition (Recognizer instance)
  361.  SRINLINE (0x0211);
  362.  
  363. /* Language Model Building and Manipulation Functions */
  364. pascal OSErr SRNewLanguageModel (RecognitionSystem system, LanguageModel *model,
  365.         const void *name, Size nameLength)
  366.  SRINLINE (0x0812);
  367. pascal OSErr SRNewPath (RecognitionSystem system, LMPath *path)
  368.  SRINLINE (0x0413);
  369. pascal OSErr SRNewPhrase (RecognitionSystem system, LMPhrase *phrase,
  370.         const void *text, Size textLength)
  371.  SRINLINE (0x0814);
  372. pascal OSErr SRNewWord (RecognitionSystem system, LMWord *word,
  373.         const void *text, Size textLength)
  374.  SRINLINE (0x0815);
  375.  
  376. /* Operations on any object of the LanguageObject family */
  377. pascal OSErr SRPutLanguageObjectIntoHandle (LanguageObject lobj, Handle lobjHandle)
  378.  SRINLINE (0x0416);
  379. pascal OSErr SRPutLanguageObjectIntoDataFile (LanguageObject lobj, short fRefNum)
  380.  SRINLINE (0x0328);
  381. pascal OSErr SRNewLanguageObjectFromHandle (RecognitionSystem system, LanguageObject *lobj, Handle lObjHandle)
  382.  SRINLINE (0x0417);
  383. pascal OSErr SRNewLanguageObjectFromDataFile (RecognitionSystem system, LanguageObject *lobj, short fRefNum)
  384.  SRINLINE (0x0427);
  385. pascal OSErr SREmptyLanguageObject (LanguageObject lobj)
  386.  SRINLINE (0x0218);
  387. pascal OSErr SRChangeLanguageObject (LanguageObject lobj, const void *text, Size textLength)
  388.  SRINLINE (0x0619);
  389. pascal OSErr SRAddLanguageObject (LanguageObject base, LanguageObject addon)
  390.  SRINLINE (0x041A);
  391. pascal OSErr SRAddText (LanguageObject base, const void *text,
  392.     Size textLength, long refCon)
  393.  SRINLINE (0x081B);
  394. pascal OSErr SRRemoveLanguageObject (LanguageObject base, LanguageObject toRemove)
  395.  SRINLINE (0x041C);
  396.  
  397. /* Utilizing the System Feedback Window */
  398. pascal OSErr SRProcessBegin (Recognizer instance, Boolean failed)
  399.  SRINLINE (0x031D);
  400. pascal OSErr SRProcessEnd (Recognizer instance, Boolean failed)
  401.  SRINLINE (0x031E);
  402. pascal OSErr SRSpeakAndDrawText (Recognizer instance, const void *text, Size textLength)
  403.  SRINLINE (0x061F);
  404. pascal OSErr SRSpeakText (Recognizer instance, const void *speakText, Size speakLength)
  405.  SRINLINE (0x0620);
  406. pascal OSErr SRStopSpeech (Recognizer recognizer)
  407.  SRINLINE (0x0223);
  408. pascal short SRSpeechBusy (Recognizer recognizer)
  409.  SRINLINE (0x0224);
  410. pascal OSErr SRDrawText (Recognizer instance, const void *dispText, Size dispLength)
  411.  SRINLINE (0x0621);
  412. pascal OSErr SRDrawRecognizedText (Recognizer instance, const void *dispText, Size dispLength)
  413.  SRINLINE (0x0622);
  414.  
  415. #ifdef    __cplusplus
  416. }
  417. #endif
  418.  
  419. #endif    /* __SPEECHRECOGNITION__ */
  420.